﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KaoQin.BLL;
using KaoQin.RFIDLIB;
using KaoQin.Model;
using KaoQin.Common;

namespace KaoQin.WPFApp.kaoQin
{
    /// <summary>
    /// ICCardMgr.xaml 的交互逻辑
    /// </summary>
    public partial class ICCardMgr : Window
    {
        public ICCardMgr()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.initData();
        }
        private void initData()
        {
            try
            {
                this.bindList();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定数据出错：" + ex.Message);
            }
        }

        private void bindList()
        {
            lstEmployee.ItemsSource = new Employee().GetModelList("KName like '%" + txtKey.Text.Trim() + "%'");
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            this.bindList();
        }

        private void lstEmployee_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btnFaKa_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(txtCardNum.Text) && txtCardNum.Text.Length == 36)
            {
                MessageBoxResult result = MessageBox.Show("该员工已绑定IC卡，是否重新发卡？", "确认", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.No)
                {
                    return;
                }
            }

            try
            {

                int state;
                CardFun cardbll = new CardFun();
                ICCardManager icMgrbll = new ICCardManager();

                Guid icCardUniqueID;
                state = cardbll.GetCardUniqueID(ConfigHelper.NewPassword, out icCardUniqueID);
                cardbll.DoBeed(50);

                if (state == 0)
                {

                    ICCardManagerInfo info = icMgrbll.GetModel(icCardUniqueID.ToString());
                    EmployeeInfo empInfo = (EmployeeInfo)gridInfo.DataContext;
                    if (info != null && info.KState == 0)
                    {
                        MessageBox.Show("该卡已被使用");
                        return;

                    }
                    else
                    {
                        if (info == null)
                        {
                            info = new ICCardManagerInfo();
                            info.ID = icCardUniqueID.ToString();
                            info.ICCardID = icCardUniqueID.ToString();
                            info.EmployeeID = empInfo.ID;
                            info.KState = 0;
                            info.CreateTime = DateTime.Now;
                            icMgrbll.Add(info);
                        }
                        else
                        {
                            info.KState = 0;
                            icMgrbll.Update(info);
                        }
                        empInfo.ICCardID = info.ID;
                        new Employee().Update(empInfo);
                        txtCardNum.Text = icCardUniqueID.ToString();


                        MessageBox.Show("发卡成功");

                    }


                }
                else
                {
                    MessageBox.Show(ConfigHelper.GetRFIDState(state));
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show("发卡失败：" + ex.Message);
            }
                

            


        }

        

        private void btnSearch2_Click(object sender, RoutedEventArgs e)
        {
            bindCardList();
        }

        private void bindCardList()
        {
            try
            {
                int recordCount;
                string strWhere = " where 1=1";
                if (!string.IsNullOrEmpty(txtUserName.Text.Trim()))
                {
                    strWhere += " and  EmployeeID in (select ID from employee where KName like '%"+txtUserName.Text.Trim()+"%') ";
                }

                if (!string.IsNullOrEmpty(txtWorkNum.Text.Trim()))
                {
                    strWhere += " and  EmployeeID in (select ID from employee where WordNum like '%"+txtWorkNum.Text.Trim()+"%')";
                }
                lsv.ItemsSource = new ICCardManager().GetListByPage("", "CreateTime", pager.PageIndex, pager.PageSize, out recordCount).Tables[0].DefaultView;
                pager.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定卡片数据出错：" + ex.Message);
            }
        }

        private void DataPager_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            bindCardList();
        }

        private void btnLoss_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("确定挂失、消挂卡吗？", "确定", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    Button btn = sender as Button;
                    string ID = btn.Tag.ToString();
                    ICCardManager bll = new ICCardManager();
                    ICCardManagerInfo model = bll.GetModel(ID);
                    if (model != null)
                    {
                        if (model.KState == 2)
                        {
                            model.KState = 0;
                        }
                        else
                        {
                            model.KState = 2;
                        }
                    }
                    bll.Update(model);
                    MessageBox.Show("挂失、消挂卡成功");
                    bindCardList();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("挂失、消挂卡出错了：" + ex.Message);
            }
        }

        private void btnbackCard_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("确定改变此卡状态吗？", "确定", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    Button btn = sender as Button;
                    string ID = btn.Tag.ToString();
                    ICCardManager bll = new ICCardManager();
                    ICCardManagerInfo model = bll.GetModel(ID);
                    if (model != null)
                    {
                        if (model.KState == 1)
                        {
                            model.KState = 0;
                        }
                        else
                        {
                            model.KState = 1;
                        }
                    }
                    bll.Update(model);
                    MessageBox.Show("状态改变成功");
                    bindCardList();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("退卡失败：" + ex.Message);
            }
        }
    }
}
